package com.situ.broadcast;

import androidx.appcompat.app.AppCompatActivity;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    
    private Button mBtnSendStaticReceiver;
    private Button mBtnSendDynamicReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mBtnSendStaticReceiver = findViewById(R.id.btn_send_static_receiver);
        mBtnSendDynamicReceiver = findViewById(R.id.btn_send_dynamic_receiver);

        // 动态注册广播，就是用代码的方式注册
        registerReceiver();

        // 发送静态注册广播
        mBtnSendStaticReceiver.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                // Android8.0后，当App targetSDK >= 26，几乎禁止了所有的隐式广播的静态注册监听
                // 就需要使用显示广播方式发送，显示广播就是指定具体的广播
                if (Build.VERSION.SDK_INT >= 26) {
                    ComponentName componentName = new ComponentName(getApplicationContext(), "com.situ.broadcast.StaticReceiver");
                    intent.setComponent(componentName);
                }
//                if (Build.VERSION.SDK_INT >= 26) {
//                    intent.addFlags(0x01000000);
//                }
                // 定义广播的事件类型
                intent.setAction("com.situ.static");
                // 给广播发送数据
                intent.putExtra("msg", "接收静态注册的广播");
                // 发送广播
                sendBroadcast(intent);
            }
        });

        // 发送自定义的动态注册广播
        mBtnSendDynamicReceiver.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                // 定义广播的事件类型
                intent.setAction("com.situ.dynamic");
                // 给广播发送数据
                intent.putExtra("msg", "接收动态注册的广播");
                // 发送广播
                sendBroadcast(intent);
            }
        });
    }

    // 动态注册广播
    private void registerReceiver() {
        IntentFilter intentFilter1 = new IntentFilter();
        intentFilter1.addAction("com.situ.dynamic");
        registerReceiver(dynamicReceiver, intentFilter1);


        IntentFilter intentFilter = new IntentFilter();
        //电量状态发送改变
        intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
        // 电量低
        intentFilter.addAction(Intent.ACTION_BATTERY_LOW);
        //电量从低变回高
        intentFilter.addAction(Intent.ACTION_BATTERY_OKAY);
        //连接充电器，也就是充电状态
        intentFilter.addAction(Intent.ACTION_POWER_CONNECTED);
        //断开充电器
        intentFilter.addAction(Intent.ACTION_POWER_DISCONNECTED);
        //监听广播
        registerReceiver(new SystemReceiver(),intentFilter);

    }

    // 动态注册的广播
    private BroadcastReceiver dynamicReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.d(TAG, "onReceive: ");
            String msg = intent.getStringExtra("msg");
            Log.d(TAG, "onReceive: " + msg);
        }
    };
}
